package com.css.fxfzypg.modules.baseData.controller;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.baseData.entity.ErcmEntity;
import com.css.fxfzypg.modules.baseData.queryParams.ErcmQueryParams;
import com.css.fxfzypg.modules.baseData.service.ErcmService;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-08-29 16:27
 * 应急处置对策管理
 */
@RestController
@RequestMapping("/fxfzypg/baseData")
@Slf4j
public class ErcmController {

    @Resource
    private ErcmService ercmService;

    @Resource
    private SysAreaService sysAreaService;

    @PostMapping("/saveErcm")
    public RestResponse saveErcm(@RequestBody ErcmEntity ercmEntity){
        RestResponse restResponse = null;

        try {

            String provinceId = ercmEntity.getProvinceId();
            String cityId = ercmEntity.getCityId();
            String prefectureId = ercmEntity.getPrefectureId();
            String address=provinceId+","+cityId+","+prefectureId;
            String areaNameByAreaId = sysAreaService.getAreaNameByAreaId(address);
            String[] split = areaNameByAreaId.split(",");
            ercmEntity.setErcmProvince(split[0]);
            ercmEntity.setErcmCity(split[1]);
            ercmEntity.setErcmPrefecture(split[2]);


            ErcmEntity ercmEntity1 = ercmService.saveErcm(ercmEntity);

            return restResponse.succeed(ercmEntity1);
        } catch (Exception e) {
            log.error("新增应急处置对策管理数据失败",e);
            return restResponse.fail("新增应急处置对策管理数据失败");
        }
    }
    @GetMapping("/getErcmById/{id}")
    public RestResponse getErcmById(@PathVariable("id")String id){
        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(ercmService.getErcmById(id));
        }catch (Exception e){
            log.error("获取应急处置对策管理数据失败!",e);
            restResponse = RestResponse.fail("获取应急处置对策管理数据失败!");
        }
        return restResponse;
    }


    @PostMapping("/updateErcm")
    public RestResponse updateErcm(@RequestBody ErcmEntity ercmEntity){
        try {
            String provinceId = ercmEntity.getProvinceId();
            String cityId = ercmEntity.getCityId();
            String prefectureId = ercmEntity.getPrefectureId();
            String address=provinceId+","+cityId+","+prefectureId;
            String areaNameByAreaId = sysAreaService.getAreaNameByAreaId(address);
            String[] split = areaNameByAreaId.split(",");
            ercmEntity.setErcmProvince(split[0]);
            ercmEntity.setErcmCity(split[1]);
            ercmEntity.setErcmPrefecture(split[2]);

            ErcmEntity ercmEntity1= ercmService.updateErcm(ercmEntity);
            return RestResponse.succeed(ercmEntity1);
        } catch (Exception e) {
            log.error("修改失败",e);
            return RestResponse.fail("修改失败");
        }
    }

    @PostMapping("/deleteErcm/{ids}")
    public RestResponse deleteErcm(@PathVariable("ids") List<String> ids){

        try {
            ercmService.deleteErcm(ids);

            return RestResponse.succeed("删除成功");
        } catch (Exception e) {
            log.error("删除失败",e);
            return RestResponse.fail("删除失败");
        }
    }

    @GetMapping("/getAllErcm")
    public RestResponse getAllErcm(HttpServletRequest request, ErcmQueryParams queryParams){
        RestResponse restResponse = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            queryParams.setCurPage(curPage);
            queryParams.setPageSize(pageSize);
            Map<String,Object> map = ercmService.getAllErcmByParams(queryParams);
            restResponse=RestResponse.succeed(map);
        }catch (Exception e){
            log.error("查询失败!",e);
            restResponse = RestResponse.fail("查询失败");
        }
        return restResponse;
    }

}
